//
//  Kylin_EssenceViewController.m
//  03-BuDeJie-2 TabBar设置
//
//  Created by Kylin on 16/1/29.
//  Copyright © 2016年 kylin. All rights reserved.
//

#import "Kylin_EssenceViewController.h"
#import "Kylin_AllTableViewController.h"
#import "Kylin_VideoTableViewController.h"
#import "Kylin_AudioTableViewController.h"
#import "Kylin_PictureTableViewController.h"
#import "Kylin_WordTableViewController.h"
#import "Kylin_TitleButton.h"

@interface Kylin_EssenceViewController()<UIScrollViewDelegate>

/**titleView*/
@property(nonatomic,weak)UIView *titleViews;

/**selectedButton*/
@property(nonatomic,weak)Kylin_TitleButton *selectedButton;

/**underLine*/
@property(nonatomic,weak)UIView *bottomLine;

/**scrollView*/
@property(nonatomic,weak)UIScrollView *scrollView;
@end
@implementation Kylin_EssenceViewController

- (void)dealloc{
    
    Kylin_LogFunc
}
- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    [self setupNavgtionBar];
    
    // 设置子控制器
    [self setupChildVC];
    
    // 添加ScorllView
    [self setupScrollView];
    
    // 添加标题控制器
    [self setupTitleViews];
    
    
    // 默认选择第一个控制器
    [self addChildViewIntoScrollView:Kylin_Zero];
}


- (void)setupTitleViews{
    
    UIView *titleViews = [[UIView alloc]init];
    titleViews.frame = CGRectMake(0, 64, self.view.width, 35);
    titleViews.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:titleViews];
    self.titleViews = titleViews;
    
    
    // 添加标题上的按钮
    [self setupTitleButtons];
    
    [self setupUnderLine];
}

#pragma mark- 添加按钮
- (void)setupTitleButtons{
    
    // 数据
    NSArray *titles = @[@"全部", @"视频", @"声音", @"图片", @"段子"];
    NSUInteger count = titles.count;
    
    // 按钮的宽高
    CGFloat buttomW = self.titleViews.width / count;
    CGFloat buttomH = self.titleViews.height;
    
    for (NSUInteger i = 0; i < count; i ++) {
        Kylin_TitleButton *buttom = [[Kylin_TitleButton alloc]init];
        
        [buttom addTarget:self action:@selector(tiltelButtomClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.titleViews addSubview:buttom];
        
        buttom.tag = i;
        
        buttom.frame = CGRectMake(i * buttomW, 0, buttomW, buttomH);
        
        [buttom setTitle:titles[i] forState:UIControlStateNormal];
    }
    

}

#pragma mark- 添加控件

- (void)setupUnderLine{
    
    // 取出标题里面的按钮
    Kylin_TitleButton *firstButton = self.titleViews.subviews.firstObject;
    
    // 下划线
    UIView *bottomLine = [[UIView alloc]init];
    CGFloat underLineH = 2;
    bottomLine.frame = CGRectMake(0, self.titleViews.height - underLineH, 70, underLineH);
    bottomLine.backgroundColor = [firstButton titleColorForState:UIControlStateSelected];
    [self.titleViews addSubview:bottomLine];
    
    self.bottomLine = bottomLine;
    
    // 默认点击第一个按钮
    [firstButton.titleLabel sizeToFit]; // 不把sizeToFit 无法计算出尺寸出来
    
    firstButton.selected = YES;
    self.selectedButton = firstButton;
    // 下划线
    self.bottomLine.width  = firstButton.titleLabel.width + 8;
    self.bottomLine.centerX = firstButton.centerX;
    
    
}
/**
 *  添加scrollView
 */
- (void)setupScrollView{
    // 不要自动调整scrollView的内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = self.view.bounds;
    
    // 开启分页
    scrollView.pagingEnabled = YES;
    
    // 去除边缘弹簧效果
    scrollView.bounces = NO;
    
    scrollView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    scrollView.delegate = self;
    scrollView.scrollsToTop = NO;
    

    scrollView.contentSize =CGSizeMake(self.childViewControllers.count * scrollView.width, 0);
}

/**
 *  添加s所有子控制器
 */
- (void)setupChildVC{
    
    [self addChildViewController:[[Kylin_AllTableViewController alloc]init]];
    [self addChildViewController:[[Kylin_VideoTableViewController alloc]init]];
    [self addChildViewController:[[Kylin_AudioTableViewController alloc]init]];
    [self addChildViewController:[[Kylin_PictureTableViewController alloc]init]];
    [self addChildViewController:[[Kylin_WordTableViewController alloc]init]];
}
/**
 *  设置导航栏
 */
- (void)setupNavgtionBar{
    
     self.view.backgroundColor = Kylin_CommonBgColor;
    
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    UIBarButtonItem *leftButton = [UIBarButtonItem itemWithImage:@"nav_item_game_icon" hightImage:@"nav_item_game_click_icon" target:self action:nil];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    UIBarButtonItem *rightButton = [UIBarButtonItem itemWithImage:@"navigationButtonRandom" hightImage:@"navigationButtonRandomClick" target:self action:nil];
    self.navigationItem.rightBarButtonItem = rightButton;
}


#pragma mark- 点击事件

- (void)tiltelButtomClick:(Kylin_TitleButton *)buttom{
    
    if (self.selectedButton == buttom) {
        [[NSNotificationCenter defaultCenter]postNotificationName:Kylin_titleButtonDidRepeatClickNotification object:nil];
    }
    
    [self dealTitleButtonClick:buttom];
    
}

#pragma mark- 处理按钮正常点击
- (void)dealTitleButtonClick:(Kylin_TitleButton *)titleButton{
    self.selectedButton.selected = NO;
    titleButton.selected = YES;
    self.selectedButton = titleButton;
    
    // 取出按钮的TAG 计算滚动范围
    NSInteger index = titleButton.tag;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.bottomLine.width  = titleButton.titleLabel.width + 8;
        self.bottomLine.centerX = titleButton.centerX;
        
        self.scrollView.contentOffset = CGPointMake(index * self.scrollView.width, 0);
        
    }completion:^(BOOL finished) {
        
        [self addChildViewIntoScrollView:index];
    }];
    
#pragma mark- 处理点击状态栏TableView滚动到TOP
    for (NSInteger i = 0; i < self.childViewControllers.count; i ++) {
        UIViewController *childVc = self.childViewControllers[i];
        
        // 如果当前控制器没有创建 直接返回
        if (!childVc.isViewLoaded) continue;
        UIView *childView = childVc.view;
        if ([childView isKindOfClass:[UIScrollView class]]) { // 包涵UIScrollView的子控件
            UIScrollView *scrollView = (UIScrollView *)childView;
            scrollView.scrollsToTop = (i == index);
            
            // 当这个控制器是索引是取出的控制器的时侯 当前控制器就可以执行 scrollsToTop
        }
    }

}
- (void)addChildViewIntoScrollView:(NSInteger)index{
    UIViewController *childView = self.childViewControllers[index];
    [self.scrollView addSubview:childView.view];
    
//    位置尺寸
    childView.view.y = 0;
    childView.view.x = index * self.scrollView.width;
    childView.view.height = self.scrollView.height;
    
    
}
- (void)essenceClick{
   
    Kylin_LogFunc
}


#pragma mark - <UIScrollViewDelegate>


// 当scrollView 滚动完成调用该方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
   
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    // 取出当前位置的按钮
    Kylin_TitleButton *titleButton = self.titleViews.subviews[index];
    
    [self dealTitleButtonClick:titleButton];
}

@end
